package com.webwork.mood.system.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.mood.common.dao.impl.SimpleDao;
import com.webwork.mood.system.dao.BranchDao;
import com.webwork.mood.system.dataobject.Branch;
import com.webwork.mood.util.ColumnLimit;
import com.webwork.mood.util.DBSimpleSQL;
import com.webwork.mood.util.StrManager;

@Repository
public class BranchDaoImpl extends SimpleDao implements BranchDao {

	public BranchDaoImpl() {
		super();
		this.dbTable = "t_branch";
		dbColumns.clear();
		dbColumns.put("branchId", "BRANCH_ID");
		dbColumns.put("branchName", "BRANCH_NAME");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof Branch))
			return new ColumnLimit();
		Branch o = (Branch) object;
		ColumnLimit ret = new ColumnLimit();
		if (!StrManager.isEmpty(o.getBranchId())) {
			ret.add(dbColumns.get("branchId") + " = ");
		}
		if (!StrManager.isEmpty(o.getBranchName())) {
			ret.add(dbColumns.get("branchName") + " like ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		Branch o = (Branch) object;
		List<Object> ret = new ArrayList<Object>();
		if (!StrManager.isEmpty(o.getBranchId())) {
			ret.add(o.getBranchId());
		}
		if (!StrManager.isEmpty(o.getBranchName())) {
			ret.add("%" + o.getBranchName() + "%");
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		Branch o = (Branch) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getBranchId());
		objs.add(o.getBranchName());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		Branch ret = new Branch();
		ret.setBranchId((String) map.get(dbColumns.get("branchId")));
		ret.setBranchName((String) map.get(dbColumns.get("branchName")));
		return ret;
	}
	
	@Override
	public void insert(Branch o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void update(Branch o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("branchId") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getBranchId());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public void delete(String branchId) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("branchId") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		this.getJdbcTemplate().update(sql, branchId);
	}

	@Override
	public List<Branch> query(Branch o, int page, int size) {
		List<Branch> ret = new ArrayList<Branch>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((Branch) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(Branch o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}
	
}
